﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text;
using System.Resources;
using WebCrawler.ProcessingFiles.CategoryClasses;

namespace WebCrawler.ProcessingFiles
{
    public partial class PredictProducts : System.Web.UI.Page
    {
        Exception ex = new Exception();
        public String errorPage = "PredictProducts";

        public string BaseLink = "http://www.newegg.com/Product/Product.aspx?Item=";
        public string BasePattern = "N82E1";
       

        protected void Page_Load(object sender, EventArgs e)
        {
            timer_Page.Interval = 10000000;

            if (!Page.IsPostBack)
            {
                try
                {
                        ProcessPattern();
                        
                   
                }
                catch(Exception ex) 
                {
          

                      ProcessErrorText(ex);

                }
               
            }
        }

        protected void ProcessPattern()
        {
            ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();

            int query = (from p in dc.PredictProducts select p).Count();
            long currCount = 0;

            if (query > 0)
            {

                currCount = 100000 - query;

                if (currCount <= 0)
                {
                    
                    CheckParseLink();
                }
            }
            else
            {
                currCount = 100000;
                
            }
            
            long startNode = 6840998009;
            long EndNode = startNode+currCount;
            long i = startNode;

            while(i < EndNode)
            {
                GetParseResult(i.ToString());
                i++;
            }
            
        }

        protected void GetParseResult(string currentNode)
        {
            string currURL = BaseLink + BasePattern + currentNode;

            ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();

             var query = from p in dc.PredictProducts where p.predictURL == currURL.Trim() select p;

             if (query.Count() == 0)
             {

                 PredictProduct pp = new PredictProduct();
                 pp.predictURL = currURL;
                 pp.predictResult = 0;

                 dc.PredictProducts.InsertOnSubmit(pp);
                 dc.SubmitChanges();
             }
        }

        protected void CheckParseLink()
        {
            try
            {

            ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();

            var query = from p in dc.PredictProducts where p.predictResult == 0 select p;

            if (query.Count() > 0)
            {
                
                    foreach (var item in query)
                    {

                        string strResult = "";
                        string currURL = item.predictURL;

                        WebResponse objResponse;
                        WebRequest objRequest = System.Net.HttpWebRequest.Create(currURL);

                        objResponse = objRequest.GetResponse();

                        using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
                        {
                            strResult = sr.ReadToEnd();
                            sr.Close();
                        }

                        strResult = strResult.Replace("\"", "");
                        strResult = strResult.Replace("#", "");
                        strResult = strResult.Replace("&", "|");

                        strResult = strResult.Remove(0, 10000);


                        if (strResult.Contains("Spec"))
                        {
                            item.predictResult = 1;
                            dc.SubmitChanges();

                        }
                        else
                        {

                            item.predictResult = 2;
                            dc.SubmitChanges();
                        }



                    }

                }
                else
                {
                    timer_Page.Interval = 2000;
                }
            }
            catch (Exception ex)
            {


                ProcessErrorText(ex);

            }
        }

        protected void ProcessErrorText(Exception ex)
        {

            StringBuilder errorText = new StringBuilder();

            if (ex != null)
            {
                if (ex.Source != null)
                {
                    errorText.Append(errorPage + " :source: " + ex.Source);
                }
                if (ex.Message != null)
                {
                    errorText.Append(errorPage + " :message: " + ex.Message);
                }
                if (ex.InnerException != null)
                {
                    errorText.Append(errorPage + " :innerexception: " + ex.InnerException);
                }
                if (ex.TargetSite != null)
                {
                    errorText.Append(errorPage + " :target: " + ex.TargetSite);
                }

            }
            else
            {
                errorText.Append("unknown error");
            }

            ProductDataDataContextDataContext dce = new ProductDataDataContextDataContext();

            WebCrawler wc = new WebCrawler();
            wc.webCrawlerErrorText = errorText.ToString();

            dce.WebCrawlers.InsertOnSubmit(wc);
            dce.SubmitChanges();
        }

        protected void GetNextPage(object sender, EventArgs e)
        {
            ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();

            var query = from p in dc.PredictProducts where p.predictResult == 0 select p;

            if (query.Count() == 0)
            {
                Response.Redirect("/ProcessingFiles/ProcessOneProduct.aspx");
            }
        }
    }
}